﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using System.Data;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Enterprise.RelationQuery
{
    public partial class RegCompanyAndArea : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hidStatus.Value = "0";
                setBind();
            }
        }

        IBLL_Area iArea = Factory.BLL_Area();
        IBLL_SelectArea iSelectArea = Factory.BLL_SelectArea();
        /// <summary>
        /// 绑定所有符合条件的采购目录
        /// </summary>
        private void setBind()
        {

            Model.Area aArea = CurrentAccount.GetCurrentDeployArea();  //获取当前实施地区的地区

            IList<Model.Area> aAreaList = iArea.GetAllCity(aArea.AreaID);
            gvwArea.DataSource = aAreaList;
            gvwArea.DataBind();


        }


        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string code = this.txtCode.Text;
            if (CurrentAccount.VerifyValidateCode(code))
            {
                if (new HZ.Common.ConvertHelper(txtcompanyname.Text.Trim()).String != string.Empty && new HZ.Common.ConvertHelper(txtCellNo.Text.Trim()).String != string.Empty && new HZ.Common.ConvertHelper(txtContactPerson.Text.Trim()).String != string.Empty)
                {
                    CompanyInfo compinfo = new CompanyInfo();
                    //  compinfo.CompanyID = Guid.NewGuid().ToString();
                    compinfo.CompanyName = txtcompanyname.Text.Trim();
                    compinfo.TelNo = txtTelNo.Text.Trim();
                    compinfo.ContactPerson = txtContactPerson.Text.Trim();
                    compinfo.CellNo = txtCellNo.Text.Trim();
                    compinfo.LayMan = txtLayMan.Text.Trim();
                    compinfo.Address = txtAddress.Text.Trim();
                    compinfo.BusinessCertificate = txtBusinessCertificate.Text.Trim();
                    compinfo.BusinessCertificateDate = new HZ.Common.ConvertHelper(txtBusinessCertificateDate.Text.Trim()).ToDateTime;
                    compinfo.Website = txtWebsite.Text.Trim();
                    compinfo.GSPCode = txtGSPCode.Text.Trim();
                    compinfo.GSPTime = new HZ.Common.ConvertHelper(txtGSPTime.Text.Trim()).ToDateTime;
                    compinfo.BusinessRange = txtBusinessRange.Text.Trim();
                    compinfo.Remarks = txtRemarks.Text.Trim();
                    compinfo.AddpersonID = CurrentAccount.GetUser().Org.OrgID;


                    SelectArea sArea = new SelectArea();
                    //获取所有选择的目录
                    string ids = ",";

                    //遍历列表的所有行
                    for (int _index = 0; _index < gvwArea.Rows.Count; _index++)
                    {

                        CheckBox ckb = ((CheckBox)gvwArea.Rows[_index].FindControl("checkbox1"));
                        if (ckb.Checked)
                        {
                            ids += this.gvwArea.DataKeys[_index].Value.ToString() + ",";
                        }
                    }

                    sArea.AreaIDs = ids;
                    sArea.CompanyID_PS = "";

                    IBLL_Company company = Factory.BLL_Company();
                    int result = company.RegNewCompany(compinfo, sArea);

                    if (result == 1)
                    {
                        WebHint.ShowInfo("提交成功！", "CompanyMaintain.aspx");
                    }
                    else if (result == 2)
                    {
                        HZ.Web.WebUtil.Alert("“企业名称”或“经营许可证号”已经存在！", this);
                    }
                    else
                    {
                        WebHint.ShowError("提交失败！", "");
                    }
                }
                else
                {
                    HZ.Web.WebUtil.Alert("请填写必填项！", this);
                }
            }
            else
            {
                this.txtCode.Text = "";
                HZ.Web.WebUtil.Alert("验证码不正确！", this);
            }
        }

    }
}
